﻿
/* $dhUsers */
//依赖layer
(function ($) {

    var $dhUsers = {};
    $dhUsers.userData = [];
    
    $dhUsers.init = function () {
       
        if ($('link[ref$="jquery.tree-multiselect.css"]').length == 0) {
            {
                $('<link href="/content/treeMultiselect/jquery.tree-multiselect.css" rel="stylesheet" type="text/css" />').appendTo('head');
                $('<script src= "/content/treeMultiselect/jquery.tree-multiselect.js" type= "text/javascript" />').appendTo('head');
            }
        }

        //$dhUsers.initUserData(function (result,data) {
        //    if (result) {
        //        $dhUsers.userData = data;
        //        $dhUsers.openWin();
        //    }
        //});

        $dhUsers.openWin();


    }

    $dhUsers.initUserData = function (callback) {
       
        $.ajax({
            type: "GET",
            url: "/User/GetUtiltityUsers",
            dataType: "json",
            success: function (data) {
                callback(true,data);
            }, error: function (xhr, textStatus, errorThrown) {
                callback(false,null);
            }
        });
    }

    $dhUsers.openWin = function (callback) {

        var usersDiv = '<select id="userSelects" ></select>';

        //$.each($dhUsers.userData, function (i) {
        //    var opEle = '<option value="' + $dhUsers.userData[i].userId + '" data-section="' + $dhUsers.userData[i].orgName + '">' + $dhUsers.userData[i].name + '</option>';
        //    $('select#userSelects').append(opEle);
        //});


        //var options = { showSectionOnSelected: false, startCollapsed: true, searchable: true, searchParams: ['section', 'text'] };
        //$("select#userSelects").treeMultiselect(options);
        //var firstEle = $('.auxiliary').next().removeClass('collapsed');

        $.ajax({
            type: "GET",
            url: "/User/GetUtiltityUsers",
            dataType: "json",
            success: function (data) {
                $.each(data, function (i) {
                    var opEle = '<option value="' + data[i].userId + '" data-section="' + data[i].orgFullName + '">' + data[i].name + '</option>';
                    $('#userSelects').append(opEle);
                });


                var options = { showSectionOnSelected: false, startCollapsed: true, searchable: true, searchParams: ['section', 'text'] };
                $("select#userSelects").treeMultiselect(options);
                var firstEle = $('.auxiliary').next().removeClass('collapsed');
                

                
            }, error: function (xhr, textStatus, errorThrown) {
                
            }
        });

        var layerIndex = layer.open({
            type: 1,
            title: "用户搜索", //不显示标题
            area: ['900px', '600px'], //宽高
            content: usersDiv, //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            btn: ['保存', '关闭'],
            yes: function () {
                var newPerIds = "";
                var userIds = $('.selected .item').each(function () {
                    newPerIds += $(this).attr('data-value') + ",";
                });
                newPerIds = newPerIds.substr(0, newPerIds.length - 1);

                callback(newPerIds);
            },
            no: function () {
                
            },
            success: function (layero, index) {
                
            },
            cancel: function () {

            }
        });
    }

    $dhUsers.prototype.onSaveUsers = function () {

    }



    window.$dhUsers = $dhUsers;
})($);